package com.dsj.gxrd.modules.activity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.activity.domain.entity.RdOverview;
import com.dsj.gxrd.modules.activity.domain.vo.RdOverviewVo;
import com.dsj.gxrd.modules.activity.mapper.RdOverviewMapper;
import com.dsj.gxrd.modules.activity.service.RdOverviewService;
import com.dsj.gxrd.utils.BaseEntityHandlerUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author songpin566
 * @date 2023/10/16
 **/
@Service
public class RdOverviewServiceImpl extends ServiceImpl<RdOverviewMapper, RdOverview> implements RdOverviewService {


    @Override
    public PageInfo<RdOverviewVo> selectOverviewList(String name, String isPublish, String rdBookCatId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RdOverview> list = this.lambdaQuery()
                .like(StringUtils.isNotEmpty(name), RdOverview::getName, name)
                .eq(RdOverview::getIsPublish, isPublish)
                .eq(StringUtils.isNotEmpty(rdBookCatId), RdOverview::getRdBookCatId, rdBookCatId)
                .select(RdOverview::getName,
                        RdOverview::getPosition,
                        RdOverview::getId,
                        RdOverview::getIsPublish,
                        RdOverview::getContent,
                        RdOverview::getHeadImgUrl).list();
        return ((Page<?>) list).toPageInfo(item -> BeanUtils.copyProperties(item, RdOverviewVo.class));
        //List<RdOverviewVo> rdOverviewVos = BeanUtils.copyProperties(list, RdOverviewVo.class);
        //PageInfo<RdOverviewVo> target = new PageInfo<>();
        //BeanUtils.copyProperties(source,target);
        //target.setList(rdOverviewVos);
    }

    @Override
    public boolean insertOverview(RdOverview overview) {
        overview.setDelFlag(0);
        BaseEntityHandlerUtils.fastSetBaseEntity(overview, SecurityUtils.getLoginUser(), false);
        return save(overview);
    }

    @Override
    public boolean updateOverview(RdOverview overview) {
        BaseEntityHandlerUtils.fastSetBaseEntity(overview, SecurityUtils.getLoginUser(), true);
        return updateById(overview);
    }

    @Override
    public AjaxResult removes(List<Long> ids) {
        return removeBatchByIds(ids) ? AjaxResult.success("删除成功") : AjaxResult.error();
    }

    @Override
    public RdOverview getInfo(Long id) {
        return this.baseMapper.selectById(id);
    }


}
